const BASE_URL = "https://www.xlshhr.com/beauty-industry";
// const BASE_URL = "http://8.129.113.116:8081/beauty-industry";
// const BASE_URL = "http://8.129.113.116:8082/beauty-industry";




/**
 * Request请求方法
 * @param  {String} url    链接
 * @param  {Objece} params 参数
 * @param  {Boolean} isToken  是否携带token
 * @return {Promise}       包含抓取任务的Promise
 */
function getApi(url, params, isToken, isbinding, isshowerror) {

  if (isToken === undefined) isToken = true;
  if (isbinding === undefined) isbinding = true;
  if (isshowerror === undefined) isshowerror = false;


  let token = wx.getStorageSync('token') || '';
  let uKey = wx.getStorageSync('uKey') || '';

  const promise = new Promise((resolve, reject) => {
    //判断是否需要置入token
    if (isToken) params = Object.assign({
      sessionId: token
    }, params)
    wx.request({
      url: `${BASE_URL}${url}`,
      method: 'POST',
      data: params, //置入token
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'token': token,
        'uKey': uKey
      },
      success: res => {
        if (res.statusCode == 200) {
          if (res.data.code == 1) {
            resolve(res);
          } else if (res.data.code == 2) {
            wx.navigateTo({
              url: '/pages/login/index',
            })
          } else if (res.data.code == 5) {
            wx.showModal({
              title: "您还没有开通店铺功能",
              confirmText: "前往开通",
              success: (res) => {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '/pages/Index/bill/bill?type=0',
                  })
                }
              }
            })
          } else {
            wx.showToast({
              title: res.data.desc,
              icon: 'none',
              duration: 2000
            })
          }


        } else {
          wx.showToast({
            title: '[' + res.code + '] 服务器出错,请重试',
            icon: 'none',
            duration: 2000
          });
          reject();
        }
      },
      fail: err => {
        wx.showToast({
          title: '当前网络不给力,请刷新重试',
          icon: 'none',
          duration: 2000
        });
        reject();
      },
      complete: res => {

      }
    });
  });

  if (token == '' && isToken) {
    return new Promise((resolve, reject) => {
      wx.showToast({
        title: '状态失效，请关闭小程序后，重新打开',
        icon: 'none'
      });
    });
  } else {
    return promise;
  }
}

function requestApi(url, params) {
  params = params || {};
  let sessionId = wx.getStorageSync('token');
  let shopId = wx.getStorageSync('shopId');
  if (!params.sessionId) {
    params.sessionId = sessionId;
  }
  if (!params.shopId) {
    params.shopId = shopId;
  }

  return new Promise((resolve, reject) => {
    getApi(url, params).then((res) => {
      resolve(res.data.data, res.data);
    }, (res) => {
      reject(res.data);
    });
  });
}



module.exports = {
  get: getApi,
  post: getApi,
  requestApi: requestApi,
  url: BASE_URL
}

// let request = new Request();

// module.exports = {
//     get: request.getApi,
//     post: request.getApi,
//     requestApi:request.requestApi,
//     url: BASE_URL
// }